import { onHide } from '@dcloudio/uni-app';
<!--
 * @Description:
 * @Date: 2024-05-14 09:00:16
 * @LastEditTime: 2024-10-29 09:30:14
-->
<route lang="json5" type="login">
{
  style: {
    navigationBarTitleText: '市民登录',
  },
}
</route>
<template>
  <view class="w-screen h-screen flex flex-col justify-center items-center bg-#fff">
    <view class="w-70% flex flex-col items-center">
      <image class="w-280rpx h-180rpx" :src="leaderLogo" mode="scaleToFill" />
      <view class="text-36rpx font-550 text-#000 mt-40rpx">人民检察院</view>
      <view class="text-30rpx mt-40rpx mb-60rpx text-center text-#000">
        为保证您反馈的事项能及时的处理，请选择正确的区县哦～
      </view>
      <view class="mb-20rpx border-rd-20rpx overflow-hidden w-100% border border-solid text-#000">
        <wd-select-picker
          label="选择区县"
          v-model="county"
          :columns="countyList"
          type="radio"
          @change="handleChange"
        ></wd-select-picker>
      </view>
      <view class="w-full">
        <button
          v-if="loginCode && userStore.userInfo.tenantId"
          class="bg-#4D80F0 text-#fff text-30rpx h-80rpx rounded-20rpx"
          open-type="getPhoneNumber"
          @getphonenumber="getphonenumber"
        >
          授权并登录
        </button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { loginApi, getTenantIdByName, getIdByNames } from '@/service/index/foo'
import { useUserStore } from '@/store'
import leaderLogo from '../../static/login/leaderLoginLogo.png'
const loginCode = ref('')
const userStore = useUserStore()
const county = ref(null)
const countyList = ref([
  {
    value: '延安市院',
    label: '延安市',
  },
  {
    value: '宝塔区院',
    label: '宝塔区',
  },
  {
    value: '安塞区院',
    label: '安塞区',
  },
  {
    value: '延长县院',
    label: '延长县',
  },
  {
    value: '延川县院',
    label: '延川县',
  },
  {
    value: '子长市县院',
    label: '子长市',
  },
  {
    value: '志丹县院',
    label: '志丹县',
  },
  {
    value: '吴起县院',
    label: '吴起县',
  },
  {
    value: '甘泉县院',
    label: '甘泉县',
  },
  {
    value: '富县院',
    label: '富县',
  },
  {
    value: '洛川县院',
    label: '洛川县',
  },
  {
    value: '宜川县院',
    label: '宜川县',
  },
  {
    value: '黄龙县院',
    label: '黄龙县',
  },
  {
    value: '黄陵县院',
    label: '黄陵县',
  },
])
onShow(() => {
  uni.login({
    provider: 'weixin',
    success: function (loginRes) {
      loginCode.value = loginRes.code
    },
  })
  // getTenantIdByName('延安').then((res) => {
  //   userStore.setUserInfo({ tenantId: res.data + '' })
  // })
  getTenantIdList()
})

function getTenantIdList() {
  getIdByNames().then(async (res) => {
    county.value = userStore.userInfo.tenantId + '' || '167'
    countyList.value.length = 0
    res.data.list.forEach((item) => {
      if (item.packageId != 0) {
        countyList.value.push({
          value: item.id,
          label: item.name,
        })
      }
    })
    county.value = userStore.userInfo.tenantId
  })
}
/**
 * @description: 微信手机授权回调
 * @param {*} e
 * @return {*}
 */
function getphonenumber(e) {
  if (e.detail.errMsg == 'getPhoneNumber:ok') {
    loginApi({
      loginCode: loginCode.value,
      phoneCode: e.detail.code,
      encryptedData: e.detail.encryptedData,
      ivStr: e.detail.iv,
      tenantId: userStore.userInfo.tenantId,
    }).then((res) => {
      userStore.setUserInfo({
        token: res.data.accessToken,
        phone: res.data.phone,
        openid: res.data.openId,
        userId: res.data.userId,
        nickname: res.data.name as string,
        currentRole: 'people',
      })
      if (res.data.idCard) {
        uni.switchTab({ url: '/pages/home/index' })
      } else {
        uni.navigateTo({ url: '/pages/login/userCard' })
      }
    })
  }

  // 跳转到首页
}
function handleChange({ value }) {
  userStore.setUserInfo({ tenantId: value })
}
</script>
<style lang="scss" scoped>
::v-deep .wd-button {
  width: 100%;
}
</style>
